package com.dhcc.bpm.modules.system.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.entity.Role;
import com.dhcc.bpm.modules.system.entity.User;
import com.dhcc.bpm.modules.system.service.IRoleService;
import com.dhcc.bpm.modules.system.service.IUserService;
import com.dhcc.bpm.modules.system.vo.GrantVO;
import com.dhcc.bpm.modules.system.vo.RoleVO;
import com.dhcc.bpm.modules.system.wrapper.RoleWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.constant.AuthConstant;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
/**
 * @ClassName ParamController
 * @Description 角色管理 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/role")
@Api(value = "角色", tags = "角色")
@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public class RoleController extends BladeController {

	private IRoleService roleService;
	private IUserService userService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param role
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.RoleVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入role")
	@ApiLog("查询角色详情")
	@Log(title = "查询角色详情", businessType = BusinessType.SELECT)
	public R<RoleVO> detail(Role role) {
		Role detail = roleService.getOne(Condition.getQueryWrapper(role));
		return R.data(RoleWrapper.build().entityVO(detail));
	}

	/**
	 * @Description 列表
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param role
	 * @return org.springblade.core.tool.api.R<java.util.List<org.springblade.core.tool.node.INode>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleName", value = "参数名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "roleAlias", value = "角色别名", paramType = "query", dataType = "string")
	})
	@PreAuth(AuthConstant.HAS_ROLE_ADMIN)
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入role")
	@ApiLog("查询角色列表")
	@Log(title = "查询角色列表", businessType = BusinessType.SELECT)
	public R<List<INode>> list(@ApiIgnore @RequestParam Map<String, Object> role) {
		List<Role> list = roleService.list(Condition.getQueryWrapper(role, Role.class).lambda().orderByAsc(Role::getSort));
		return R.data(RoleWrapper.build().listNodeVO(list));
	}

	/**
	 * @Description 获取角色树形结构
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param tenantId
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.vo.RoleVO>>
	 **/
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	@ApiLog("获取角色树形结构")
	@Log(title = "获取角色树形结构", businessType = BusinessType.SELECT)
	public R<List<RoleVO>> tree(String tenantId, BladeUser bladeUser) {
		List<RoleVO> tree = roleService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
		return R.data(tree);
	}

	/**
	 * @Description 新增或修改
	 * @Author sjm
	 * @Date 10:06 2020/2/22 0022
	 * @param role
	 * @param user
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入role")
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiLog("新增或修改角色")
	@Log(title = "角色", businessType = BusinessType.INSERT)
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R submit(@Valid @RequestBody Role role, BladeUser user) {
		if (Func.isEmpty(role.getId())) {
			role.setTenantId(user.getTenantId());
		}
		return R.status(roleService.submit(role));
	}

	/**
	 * @Description 删除
	 * @Author sjm
	 * @Date 10:07 2020/2/22 0022
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "删除", notes = "传入ids")
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@ApiLog("删除角色")
	@Log(title = "角色", businessType = BusinessType.DELETE)
	@Transactional(rollbackFor = Exception.class)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		boolean flag = false;
		List<Long> list = Func.toLongList(ids);
		for(Long id : list){
			Role role = roleService.getById(id);
			List<Role> roleList = roleService.getByParentId(role.getId().toString());
			if(roleList.size() > 0){
				return R.fail(222, "请先删除子节点！");
			}
			List<User> userList = userService.getByRoleId(role.getId().toString());
			if(userList.size() > 0){
				return R.fail(222, "有用户使用该角色，无法删除！");
			}
			flag = roleService.removeByRoleId(id);
		}
		return R.status(flag);
	}

	/**
	 * @Description 设置角色权限
	 * @Author sjm
	 * @Date 10:07 2020/2/22 0022
	 * @param grantVO
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/grant")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "权限设置", notes = "传入roleId集合以及menuId集合")
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiLog("设置角色权限")
	@Log(title = "角色", businessType = BusinessType.UPDATE)
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R grant(@RequestBody GrantVO grantVO) {
        boolean temp = roleService.grant(grantVO.getRoleIds(), grantVO.getMenuIds(), grantVO.getDataScopeIds(), grantVO.getApiScopeIds());
		return R.status(temp);
	}

}
